package com.wuyan.locationdog.ui.device


import androidx.activity.viewModels
import androidx.core.view.isVisible
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.DeviceBeanContent
import com.arcsoft.commonbase.bean.DogFileBeanContent
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_device_info_modify
import com.arcsoft.commonbase.bus.bus_dog_file_info
import com.arcsoft.commonbase.ext.customShowLong
import com.arcsoft.commonbase.ext.gone
import com.arcsoft.commonbase.ext.loadImageCircleCar
import com.arcsoft.commonbase.ext.visible
import com.blankj.utilcode.util.ActivityUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.setDogDesc
import com.wuyan.locationdog.databinding.DeviceSetActivityBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.help.DogHelp
import com.wuyan.locationdog.ui.my.MyModifyPwdActivity
import com.wuyan.locationdog.viewmodel.DeviceViewModel

class DeviceSetActivity : BaseBindingActivity<DeviceSetActivityBinding>() {
    private var bindDogId: String? = null
    private lateinit var deviceBeanContent: DeviceBeanContent
    private val mViewModel: DeviceViewModel by viewModels()

    override fun initData() {
        deviceBeanContent = intent.getSerializableExtra("DeviceBeanContent") as DeviceBeanContent

        setTitleText("设备详情")


        binding.apply {

            btnAddDogInfo.setOnClickListener {
                DogHelp.isGoDetail = false
                ActivityUtils.startActivity(DogFilesListActivity::class.java)
            }
            btnUnbind.setOnClickListener {
                bindDogId = null
                binding.apply {
                    btnDogInfo.gone()
                    btnAddDogInfo.visible()
                }
            }
            btnSure.setOnClickListener {
                if (btnAddDogInfo.isVisible) {
                    if (deviceBeanContent.dog?.dogId.isNullOrEmpty()) {
                        customShowLong("请绑定猎犬")
                        return@setOnClickListener
                    }
                    val map = HashMap<String, Any>()
                    map["deviceId"] = deviceBeanContent.deviceId
                    map["dogId"] = deviceBeanContent.dog?.dogId.toString()
                    mViewModel.dogUnBind(map)
                } else {
                    if (btnDogInfo.isVisible && bindDogId.isNullOrEmpty()) {
                        //直接点击保存
                        finish()
                        return@setOnClickListener
                    }
                    if (bindDogId.isNullOrEmpty()) {
                        customShowLong("请绑定猎犬")
                        return@setOnClickListener
                    }
                    val map = HashMap<String, Any>()
                    map["deviceId"] = deviceBeanContent.deviceId
                    map["dogId"] = bindDogId.toString()
                    mViewModel.dogBind(map)
                }
            }
            deviceBeanContent.dog?.let {
                dogImg.loadImageCircleCar(it.dogImg)
                dogName.text = it.dogName
                setDogDesc(dogType, it)
//                dogType.text = it.dogType + " | " + it.age + " | " + it.weight + "kg"
                if (it.gender == "公") {
                    gender.setImageResource(R.mipmap.men)
                } else {
                    gender.setImageResource(R.mipmap.women)
                }
            }
            deviceBeanContent.deviceDetailVO?.let {
                deviceId.text = it.deviceId
                imei.text = it.imei
                simNo.text = it.simNo

            }


            btnDeviceSetPwd.setOnClickListener {
                DeviceHelp.isDeviceModifyPwd = true
                ActivityUtils.startActivity(MyModifyPwdActivity::class.java)
            }

        }
    }

    private fun isShare() = deviceBeanContent.deviceDetailVO.bindType != 1

    override fun loadNetData() {
        super.loadNetData()
        val map = HashMap<String, Any>()
        map["deviceId"] = deviceBeanContent.deviceId
        mViewModel.deviceDetail(map)
    }

    override fun observe() {
        super.observe()
        Bus.observe<DogFileBeanContent>(bus_dog_file_info, this) {
            bindDogId = it.dogId
            binding.apply {
                btnDogInfo.visible()
                btnAddDogInfo.gone()

                dogImg.loadImageCircleCar(it.dogImg)
                dogName.text = it.dogName
                setDogDesc(dogType, it)

//                dogType.text = it.dogType + " | " + it.age + " | " + it.weight + "kg"
                if (it.gender == "公") {
                    gender.setImageResource(R.mipmap.men)
                } else {
                    gender.setImageResource(R.mipmap.women)
                }
            }
        }
        mViewModel.anyBean.observe(this) {
            Bus.post(bus_device_info_modify, "")
            finish()
        }


        //解绑
        mViewModel.removeSuccess.observe(this) {
            Bus.post(bus_device_info_modify, "")

            binding.apply {
                btnDogInfo.gone()
                btnAddDogInfo.visible()
            }
            finish()
        }
        mViewModel.deviceBeanContent.observe(this) {
            binding.apply {
                if (it.dog == null) {
                    btnDogInfo.gone()
                } else {
                    btnAddDogInfo.gone()
                    btnDogInfo.visible()
                }
                it.dog?.let {
                    dogImg.loadImageCircleCar(it.dogImg)
                    dogName.text = it.dogName
                    setDogDesc(dogType, it)

//                    dogType.text = it.dogType + " | " + it.age + " | " + it.weight + "kg"
                    if (it.gender == "公") {
                        gender.setImageResource(R.mipmap.men)
                    } else {
                        gender.setImageResource(R.mipmap.women)
                    }
                }

                if (isShare()) {
                    btnUnbind.gone()
                    btnAddDogInfo.gone()
                    otherSet.gone()
                    btnSure.gone()
                }

            }

        }

    }

}